import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { ConfigService } from '../../../core/common/config.service';
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
@Component({
      selector: 'app-settlement',
      templateUrl: './settlement.component.html',
      styleUrls: ['./settlement.component.scss']
})
export class SettlementComponent implements OnInit {
      public total_id: string;
      public total_chnl_id: string;
      public settlements: any;
      public left_detail: any;
      public right_detail: any;
      public adjust_fee: any;
      public orderId: any;
      public detail: any;
      public count: any = 0;
      public zong: any = 0;
      constructor(public configService: ConfigService, public http: Http, public activatedRoute: ActivatedRoute, public router: Router) {

      }
      ngOnInit() {
            this.total_id = this.activatedRoute.snapshot.params['ttid'];
            this.total_chnl_id = this.activatedRoute.snapshot.params['ttchid'];
            // console.log(this.total_id);
            // console.log(this.total_chnl_id);
            this.initTableData();
      }
      public initTableData(): void {
            const url = this.configService.get('url') + '?service=ChnlBillService&method=getChnlDetailBillInfo';
            // let url = "assets/settlement.json";
            this.http.post(url, { 'total_id': this.total_id, 'total_chnl_id': this.total_chnl_id }).toPromise().then(data => {
                  this.settlements = data.json().info;
                  // console.log(this.settlements);
                  this.orderId = this.settlements.total_id;
                  this.detail = this.settlements.detail_bills;
                  this.left_detail = this.settlements.chnl1;
                  this.right_detail = this.settlements.chnl2;
                  this.adjust_fee = this.settlements.adjust_fee;
                  // console.log(this.adjust_fee);
                  this.zong += parseFloat(this.adjust_fee);
                  // console.log(this.zong);
                  // console.log(this.detail);
                  const me = this;
                  this.detail.forEach(function (item, i) {
                        me.count += parseFloat(item.fee);
                  });
                  this.zong += this.count;
                  // console.log(this.count);
                  // console.log(this.zong);
            });
      }
      public calculateGroupTotal(chnl_name: string) {
            let total = 0;
            if (this.detail) {
                  for (const dt of this.detail) {
                        if (dt.chnl_name === chnl_name) {
                              total += parseFloat(dt.fee);
                        }
                  }
            }
            return total;
      }
      public goback(): void {
            this.router.navigate(['/account/settle']);
      }
      public toprint() {
            window.print();
      }
}
